<html>
<meta charset="UTF-8" />
<title>My 构建工具</title>
<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="js/cookie.js"></script>
<style>
table {
	width: 40%;
}

input {
	width: 100%;
}

select {
	width: 100%;
}
</style>
<body>
	<form id="frm" action="build/code" method="post"
		target="iframe_download">
		<table border="1px">
			<tr>
				<td colspan="2" align="center"><b>code 生成工具(By SQL)</b></td>
			</tr>
			<tr>
				<td width="30%">数据库类型</td>
				<td><select name="dataSource.dsDatabasetype">
						<option value="1">oracle</option>
						<option value="3" selected="selected">mysql</option>
				</select></td>
			</tr>
			<tr>
				<td>数据库主机IP</td>
				<td><input name="dataSource.dsServerip" value="127.0.0.1" /></td>
			</tr>
			<tr>
				<td>数据库主机端口</td>
				<td><input name="dataSource.dsPort" value="3306" /></td>
			</tr>
			<tr>
				<td>数据库实例名称</td>
				<td><input name="dataSource.dsInstance" value="test" /></td>
			</tr>
			<tr>
				<td>用户名</td>
				<td><input name="dataSource.dsUsername" value="root" /></td>
			</tr>
			<tr>
				<td>密码</td>
				<td><input type="password" name="dataSource.dsPassword"
					value="123456" /></td>
			</tr>
			<tr>
				<td>基础包名称</td>
				<td><input name="basePackName" value="cn.lml.generation" /></td>
			</tr>
			<tr>
				<td>beanName</td>
				<td><input name="customBeanName" value="ceshi" /></td>
			</tr>
			<tr>
				<td>selectSql</td>
				<td><textarea name="selectSql" style="width: 100%"></textarea></td>
			</tr>
			<tr>
				<td colspan="2" align="center"><input type="reset" value="重置"
					style="width: 43%" /> <input type="button" id="code" value="生成代码"
					style="width: 43%" /> </td>
			</tr>
		</table>
	</form>
	<iframe id="hidden_frame" name="iframe_download" hidden="false"></iframe>
	<pre id="resultText" style="border:2px solid grey;padding:10px;overflow:auto;overflow-y:hidden" hidden="true"></pre>
</body>
<script type="text/javascript">
	var cookieNamePrefix="pSQL_",submitInfoSymbol="(#)";
	$("#code").click(function() {
		$.ajax({
			url:"build/generateBySql",
			data:$("#frm").serialize(),
			type:'POST',
			success:function(result){
				$("#resultText").text(result);
	   			if(result!='') $("#resultText").show();
			}
		});
		setCookie(cookieNamePrefix,getSubmitInfo(),"d10000");
		setCookie(cookieNamePrefix+$("select[name='dataSource.dsDatabasetype']").val(),getSubmitInfo(),"d10000");
	});
	$("select[name='dataSource.dsDatabasetype']").change(function(){
		var str=getCookie(cookieNamePrefix+$(this).val());
		initFormInfo(str);
	});
	
	function getSubmitInfo(){
		return $("select[name='dataSource.dsDatabasetype']").val()+submitInfoSymbol+
					$("input[name='dataSource.dsServerip']").val()+submitInfoSymbol+
					$("input[name='dataSource.dsPort']").val()+submitInfoSymbol+
					$("input[name='dataSource.dsInstance']").val()+submitInfoSymbol+
					$("input[name='dataSource.dsUsername']").val()+submitInfoSymbol+
					$("input[name='dataSource.dsPassword']").val()+submitInfoSymbol+
					$("input[name='basePackName").val()+submitInfoSymbol+
					$("input[name='customBeanName").val();
	}
	
	function initFormInfo(str){
		if(str==null) return;
		var arr=str.split(submitInfoSymbol);
		if(arr.length==8){
			$("select[name='dataSource.dsDatabasetype']").val(arr[0]);
			$("input[name='dataSource.dsServerip']").val(arr[1]);
			$("input[name='dataSource.dsPort']").val(arr[2]);
			$("input[name='dataSource.dsInstance']").val(arr[3]);
			$("input[name='dataSource.dsUsername']").val(arr[4]);
			$("input[name='dataSource.dsPassword']").val(arr[5]);
			$("input[name='basePackName']").val(arr[6]);
			$("input[name='customBeanName']").val(arr[7]);
		}
	}
	initFormInfo(getCookie(cookieNamePrefix));
</script>
</html>